<%@page import="pojo.Account"%>
<%@page import="pojo.Category"%>
<%@page import="pojo.Manufacturer"%>
<%@page import="pojo.Product"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html>

<script type="text/javascript">
    ddaccordion.init({
        headerclass: "submenuheader", //Shared CSS class name of headers group
        contentclass: "submenu", //Shared CSS class name of contents group
        revealtype: "click", //Reveal content when user clicks or onmouseover the header? Valid value: "click", "clickgo", or "mouseover"
        mouseoverdelay: 200, //if revealtype="mouseover", set delay in milliseconds before header expands onMouseover
        collapseprev: true, //Collapse previous content (so only one open at any time)? true/false 
        defaultexpanded: [], //index of content(s) open by default [index1, index2, etc] [] denotes no content
        onemustopen: false, //Specify whether at least one header should be open always (so never all headers closed)
        animatedefault: false, //Should contents open by default be animated into view?
        persiststate: true, //persist state of opened contents within browser session?
        toggleclass: ["", ""], //Two CSS classes to be applied to the header when it's collapsed and expanded, respectively ["class1", "class2"]
        togglehtml: ["suffix", "<img src='Style/Admin/images/plus.gif' class='statusicon' />", "<img src='Style/Admin/images/minus.gif' class='statusicon' />"], //Additional HTML added to the header when it's collapsed and expanded, respectively  ["position", "html1", "html2"] (see docs)
        animatespeed: "fast", //speed of animation: integer in milliseconds (ie: 200), or keywords "fast", "normal", or "slow"
        oninit:function(headers, expandedindices){ //custom code to run when headers have initalized
            //do nothing
        },
        onopenclose:function(header, index, state, isuseractivated){ //custom code to run whenever a header is opened or closed
            //do nothing
        }
    })
</script>
<h2>Thêm Sản Phẩm</h2> 
<div class="warning_box">
    Cảnh Báo.
</div>
<div class="valid_box">
    Xác Nhận.
</div>
<div class="error_box">
    Lỗi.
</div> 

<div class="form"><form action="" method="post" class="niceform">

        <fieldset>
            <dl>
                <dt><label for="name">Tên sản phẩm:</label></dt>
                <dd><input type="text" name="" id="txtName" size="54" /></dd>
            </dl>
            <dl>
                <dt><label for="unitprice">Đơn giá:</label></dt>
                <dd><input type="number" name="intUPrice" id="" size="54" /></dd>
            </dl>
            <dl>
                <dt><label for="origin">Xuất xứ:</label></dt>
                <dd><input type="text" name="txtOrigin" id="" size="54" /></dd>
            </dl>

            <dl>
                <dt><label for="category">Loại sản phẩm:</label></dt>
                <dd>
                    <select size="1" name="category" id="cbCategory">
                        <c:forEach items = "${CATEGORY_LIST}" var = "p" varStatus="status">
                            <option value="${p.getId()}">${p.getName()}</option>
                        </c:forEach>
                    </select>
                </dd>
            </dl>

            <dl>
                <dt><label for="manufacturer">Hãng sản xuất:</label></dt>
                <dd>
                    <select size="1" name="manufacturer" id="cbManufacturer">
                        <c:forEach items = "${MANUFACTURER_LIST}" var = "p" varStatus="status">
                            <option value="${p.getId()}">${p.getName()}</option>
                        </c:forEach>
                    </select>
                </dd>
            </dl>
            <dl>
                <dt><label for="image">Hình ảnh:</label></dt>
                <dd><input type="file" name="upload" id="upload" /></dd>
            </dl>
            <dl class="submit">
                <input type="button" onclick="doAjax()" value="Đăng Nhập" />
            </dl>
        </fieldset>
    </form>
</div>  
<a href="#" class="bt_green"><span class="bt_green_lft"></span><strong>Add new item</strong><span class="bt_green_r"></span></a>
<a href="#" class="bt_blue"><span class="bt_blue_lft"></span><strong>View all items from category</strong><span class="bt_blue_r"></span></a>
<a href="#" class="bt_red"><span class="bt_red_lft"></span><strong>Delete items</strong><span class="bt_red_r"></span></a> 
<script type="text/javascript" src="Script/jquery-1.8.2.js"></script>
<script type="text/javascript">
    $(document).ready(function(){ 
        $(".warning_box").hide();
        $('.valid_box').hide();
        $('.error_box').hide();
        var a = "${MESSAGE}";
        if(a == "PAGE_NOT_FOUND")
        {
            $('.error_box').html("Không tìm thấy trang này.");
            $(".error_box").show("slow");
        }   
        if(a == "PAGE_EMPTY")
        {
            $('.warning_box').html("Không có sản phẩm nào trong dữ liệu.");
            $(".warning_box").show("slow");
        }           
        if(a == "PRODUCT_DELETE_SUCCESS")
        {
            $('.error_box').html("Đã xóa thành công sản phẩm.");
            $(".error_box").show("slow");
        } 
        if(a == "PRODUCT_NOT_EXIST")
        {
            $('.error_box').html("Không tìm thấy sản phẩm cần xóa.");
            $(".error_box").show("slow");
        } 
        if(a == "PRODUCT_DELETED")
        {
            $('.warning_box').html("Sản phẩm cần xóa đã bị xóa.");
            $(".warning_box").show("slow");
        }   
    })	
</script>
<script type="text/javascript">
    function doAjax() {	
        var name = $('#txtName').val();
        var price = $('#intUPrice').val();
        var origin = $('#txtOrigin').val();
        var e1 = $('#ddlViewBy').val()
        var category = e1.options[e1.selectedIndex].value;
        var e2 = $('#cbManufacturer').val();
        var manufacturer = e2.options[e2.selectedIndex].value;
        if(name == ""){
            $(".error_box_login").show("slow");
            $('.error_box_login').html("Xin nhập tên sản phẩm");
        }
        else if(price == ""){
            $(".error_box_login").show("slow");
            $('.error_box_login').html("Xin nhập đơn giá");
        }
        else if(origin == ""){
            $(".error_box_login").show("slow");
            $('.error_box_login').html("Xin nhập xuất xứ mặt hàng");
        }
        else{
            $.ajax({
                url: 'addproduct.htm',
                type: 'POST',
                data: "name=" + name + "&price=" + price + "&origin=" + origin + "&category=" + category + "&manufacturer=" + manufacturer,
                success: function(data) {
                    if(data == "NOT_EXIST")
                    {
                        $(".error_box_login").show("slow");
                        $('.error_box_login').html("Tài khoản không tồn tại");
                    }
                    else if(data == "WRONG_PASSWORD")
                    {
                        $(".error_box_login").show("slow");
                        $('.error_box_login').html("Sai mật khẩu");
                    } 
                    else if(data == "NOT_ALLOW")
                    {
                        $(".error_box_login").show("slow");
                        $('.error_box_login').html("Tài khoản của bạn không thể truy cập khu vực này");
                    }    
                    else if(data == "SUCCESS")
                    {
                        window.location = "/quanly.htm?view=quanlysanpham";
                    }
                    else
                    {
                        $(".error_box_login").show("slow");
                        $('.error_box_login').html("Lỗi kết nối. Xin thử lại sau khi reload trang");                                 
                    }
                        
                },
                error: function(data) {
                    $(".error_box_login").show("slow");
                    $('.error_box_login').html("Lỗi kết nối. Xin thử lại sau khi reload trang");                        
                }                
            });
        }
    }
</script>